<?php
$p = 'topics';
$options = array ();
CHtml::resolveNameID($model, $p, $options);

$cs = Yii::app()->clientScript;

$name = $options['name'];
$script =<<<EOL
$("#topics_selection").bind("submit", function(){
  submit_form("$name");
});
$("input[id^='adviser_']").live("click", function(){
	var id = 'node_'+jQuery(this).attr('id').replace(/adviser_/, '');
	var v = jQuery.find_topic(id);
	if (v){
		v.adviser_validate(jQuery(this).is(":checked"));
		v.adviser_checkbox().attr("disabled", false);
		v.comment_area().attr("readonly", false);
	}
});
$("input[id^='owner_']").live("click", function(){
	var id = 'node_'+jQuery(this).attr('id').replace(/owner_/, '');
	var v = jQuery.find_topic(id);
	if (v){
		v.owner_validate(jQuery(this).is(":checked"));
		v.owner_checkbox().attr("disabled", false);
		v.comment_area().attr("readonly", false);
	}
});
$("input[id^=w_]").live('focusout', function(){
	var id = 'node_'+jQuery(this).attr('id').replace(/w_/, '');
	var v = jQuery.find_topic(id);
	if (v){
		v.weeks_changed(jQuery(this).val());
	}
})

$("div[id^='weeks_layer_']").hide();
$("div[id^='comment_layer_']").hide();
$("img[id^='img_comment_']").hide();
$("li[id^='node_']").topic_validation();



EOL;

$cs->registerScript('topics_form_save', $script);

$script = '';

foreach ($model->topics as $topic => $weeks) {
	$script .= '  jQuery.tree.plugins.checkbox.check($("#node_' . $topic . '")[0]);' . "\n";
}

$script .= "after_reload()";
?>
<div class="yiiForm">

<?php $this->widget('application.components.RequiredMessage'); ?>


<?php


echo CHtml::beginForm('', 'post', array (
	'id' => "topics_selection"
));

echo Yii::t('views', 'Topics available');

$arr = $model->getHiddenFields();
foreach ($arr as $field) {
	echo CHtml::activeHiddenField($model, $field);
}

echo CHtml::activeHiddenField($model, 'form_type');
echo CHtml::errorSummary($model);

$this->Widget('application.extensions.jsTree.CjsTree', array (
	'htmlOptions' => array (
		'id' => 'topics_tree'
	),
	'ui' => array (
		'theme_name' => 'checkbox'
	),
	'options' => array (
		'plugins' => array (
			'checkbox' => array (
				'three_state' => 'true'
			)
		)
	),
	'genDiv' => false,
	'callback' => array (
		'onload' => "js:function() { \n" .
		"$script" .
		"}",
		//		'beforechange' => "js:function(NODE) { \n" .
		//		"  var v = jQuery.find_topic(jQuery(NODE).attr('id'));\n" .
		//		"  if (v)\n" .
		//		"    return !v.is_locked(NODE); \n" .
		//		"}\n",
		'oncheck' => "js:function(NODE) { \n" .
		"  var v = jQuery.find_topic(jQuery(NODE).attr('id'));\n" .
		"  if (v)\n" .
		"    v.checked(); \n" .
		"}\n",
		'onuncheck' => "js:function(NODE) { \n" .
		"  var v = jQuery.find_topic(jQuery(NODE).attr('id'));\n" .
		"  if (v)\n" .
		"    v.unchecked(); \n" .
		"}\n"
	)
));
?>

<div id="topics_tree">
<?php


displaySteps($this, $model, $route->steps);
?>
</div>

<div class="action">
<?php echo CHtml::submitButton(Yii::t('views', 'Create')); ?>
</div>

<?php echo CHtml::endForm(); ?>

</div><!-- yiiForm -->
<?php


function displaySteps($p, $model, $steps) {
	if (is_array($steps) && count($steps) > 0) {
		echo "<ul style='width:100%;'>";
		foreach ($steps as $step) {
			echo "<li id='node_" . $step->mnemonic . "' class='open node' style='width:100%;'>\n";
			echo "\t<a href='#'><ins>&nbsp;</ins>" . $step->label . "</a>";
			displayTopics($p, $model, $step->topics);
			echo CHtml::hiddenField("weeks[" . $step->mnemonic . "]", "8", array (
				'id' => 'weeks_' . $step->mnemonic,
				'size' => '3',
				'maxlength' => '3'
			));
			echo "</li>";
		}
		echo "</ul>";
	}
}

function displayTopics($p, $model, $topics) {
	if (is_array($topics) && count($topics) > 0) {
		echo "<ul style='width:100%;'>";
		foreach ($topics as $topic) {
			echo "<li id='node_" . $topic->id . "' class='node'>\n" .
			"\t<a href='#'><ins>&nbsp;</ins></a>\n\t" .
			CHtml::image('images/arrow_down_blue.png', 'Show/Hide validation comments', array (
				'onclick' => 'javascript:jQuery.toggle_comment("' . $topic->id . '")',
				'id' => 'img_comment_' . $topic->id
			)) . "\n\t" .
			"\n\t<a href='#'>" . $topic->label . "</a>\n\t" ;
					$p->widget('adviser.widgets.CommentLayer', array (
						'topic_id' => $topic->id,
						'columns' => 60,
						'rows' => 5,
						'prefix' => '',
						'adviser_validation' => isset ($model->adviser[$topic->id]),
						'adviser_topic_validation' => isset ($model->adviser_validation[$topic->id]) ? $model->adviser_validation[$topic->id] : "",
						'owner_validation' => isset ($model->owner[$topic->id]),
						'owner_topic_validation' => isset ($model->owner_validation[$topic->id]) ? $model->owner_validation[$topic->id] : "",
						'comment' => isset ($model->comment[$topic->id]) ? $model->comment[$topic->id] : "",
						'form_name' => "NewProjectForm"
					));
			displayTopics($p, $model, $topic->topics);
			echo "</li>";
		}
		echo "</ul>";
	}
}

$cs->registerScriptFile(Yii::app()->baseUrl . '/js/masks.js', CClientScript::POS_HEAD);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/old_topic_validation.js', CClientScript::POS_HEAD);
?>
